<template>
  <div class="caseComp">
    <div class="caseCompTitle">编辑项目信息</div>
    <el-form :model="info" :rules="rules" ref="ruleForm" label-width="140px">
      <el-form-item label="建设单位名称：" prop="pcName">
        <el-input
          v-model="info.pcName"
          clearable=""
          placeholder="请输入"
          class="caseCompTitleInput"
        ></el-input>
      </el-form-item>
      <el-form-item label="总承包单位名称：" prop="caseCompTitleInput">
        <el-input
          v-model="info.generalContractName"
          clearable=""
          placeholder="请输入"
          class="caseCompTitleInput"
        ></el-input>
      </el-form-item>
      <el-form-item label="分包单位名称：" prop="subContractName">
        <el-input
          v-model="info.subContractName"
          clearable=""
          placeholder="请输入"
          class="caseCompTitleInput"
        ></el-input>
      </el-form-item>
      <el-form-item label="是否政府投资项目：" prop="isGov">
        <el-select
          v-model="info.isGov"
          width="100%"
          class="caseCompTitleInput"
          placeholder="请选择"
        >
          <el-option
            v-for="item in resolveStatus"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="caseCompBtn">
      <el-button
        type="primary"
        v-sdc
        :loading="isLoading"
        class="commonBut saveBtn"
        @click="save"
        >保存</el-button
      >
      <el-button type="default" v-sdc class="commonBut" @click="finish"
        >取消</el-button
      >
    </div>
  </div>
</template>
<script>
export default {
  name: 'caseDepartment',
  props: {
    childData: {
      type: Object,
      required: true,
      default() {
        return {}
      },
    },
  },
  data() {
    return {
      isLoading: false,
      resolveStatus: [
        { value: '是', name: '是' },
        { value: '否', name: '否' },
      ],
      info: {
        id: '',
        pcName: '',
        subContractName: '',
        generalContractName: '',
      },
      initInfo: {},
      rules: {
        pcName: [{ max: 30, message: '最多输入30个字', trigger: 'blur' }],
        subContractName: [
          { max: 30, message: '最多输入30个字', trigger: 'blur' },
        ],
        generalContractName: [
          { max: 30, message: '最多输入30个字', trigger: 'blur' },
        ],
      },
    }
  },
  mounted() {
    const { id, pcName, subContractName, generalContractName, isGov } =
      this.childData
    this.info = { id, pcName, subContractName, generalContractName, isGov }
    console.log(this.info)
    this.initInfo = JSON.parse(JSON.stringify(this.info))
  },
  methods: {
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.isLoading = true
          let params = { id: this.info.id }
          let flag = true
          for (let item in this.info) {
            if (this.info[item] != this.initInfo[item]) {
              flag = false
              params[item] = this.info[item]
            }
          }
          if (flag) {
            this.finish()
            return false
          }
          this.axios
            .post('api-r/rightbackpay/expand/edit', params)
            .then((res) => {
              if (!res.data.hasError) {
                this.$message({
                  type: 'success',
                  message: res.data.msg,
                })
                this.isLoading = false
                this.finish(1)
              } else {
                this.$message({
                  type: 'warning',
                  message: res.data.errorMsg,
                })
                this.isLoading = false
              }
            })
        }
      })
    },
    finish(index) {
      this.$emit('callback', { success: index == 1 ? true : false })
    },
  },
}
</script>
<style lang="less" scoped>
.caseComp {
  border: 1px solid #dcdfe6;
  background-color: #f8f9fb;
  padding: 15px;
  &Title {
    color: #009588;
    font-size: 14px;
    line-height: 30px;
    margin-bottom: 12px;
  }
  &Btn {
    text-align: center;
    margin: 24px 0 10px;
    .el-button {
      width: 120px;
    }
  }
  /deep/ .el-radio__input.is-checked + .el-radio__label {
    color: #009588 !important;
  }
  /deep/.el-radio__input.is-checked .el-radio__inner {
    background: #009588 !important;
    border-color: #009588 !important;
  }
}
</style>
